package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
//组合注解 @ResponseBody转换为json
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

//    @RequestMapping("/dept", method = RequestMethod.GET),查看部门
    @GetMapping("/depts")
    public Result list() {
        log.info("查看全部的部门数据");

        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }
    //删除部门
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id删除部门id={}", id);
//        log.info("根据id删除部门id={}" +id);
        deptService.delete(id);

        return Result.success();
    }
    //添加部门
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept) {
    log.info("添加部门：{}", dept);
    deptService.add(dept);
    return Result.success();
    }
    //修改部门
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门：{}", dept);

        System.out.println("dept"+dept);
        deptService.update(dept); // 调用Service层的update方法来执行更新操作

        return Result.success();
    }



}
